package org.springboot.szsrcb.controller;




import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Attachment;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Test_base_info;
import org.springboot.szsrcb.entity.Test_bill;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Att_Service;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.Test_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestRestController_assign {

    private static final Logger logger = LoggerFactory.getLogger(TestRestController_assign.class);

	@Autowired
	private Req_bill_Service req_bill_Service;
	
	@Autowired
	private Test_bill_Service test_bill_Service;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private Att_Service att_Service;
	
	@RequestMapping(value = "/test_assign_page_remark")
	public String test_assign_page_remark(HttpServletRequest request) {
		String result="";
		
		logger.info("进入 test_assign_page_remark 处理");
		
		String id=request.getParameter("test_id");
		
		Test_bill r= test_bill_Service.search_a_test_bill_by_id(id);
		if (r != null) {
			result=r.getReq_remark();
		}
		return result;
	}
	
	@RequestMapping(value = "/test_assign_page_guide")
	public String test_assign_page_guide(HttpServletRequest request) {
		String result="";
		
		logger.info("进入 test_assign_page_guide 处理");
		
		String id=request.getParameter("test_id");
		
		Test_bill r= test_bill_Service.search_a_test_bill_by_id(id);
		if (r != null) {
			result=r.getTest_guide();
		}
		return result;
	}
	
	@RequestMapping(value = "/test_reject_save")
	public boolean test_reject_save(HttpServletRequest request) {
		boolean result=false;
		
		logger.info("进入 test_reject_save 处理");
		
		String id=request.getParameter("id");
		String other_reason=request.getParameter("other_reason");
		String reason_select=request.getParameter("reason_select");
		result = test_bill_Service.test_reject_save(id,other_reason,reason_select);

		return result;
	}
	
	@RequestMapping(value = "/test_assign_atts_onload")
	public String test_assign_atts_onload(HttpServletRequest request) {
		logger.info("test_assign_atts_onload");
		// model.addAttribute("name", name);
		String g_test_id= request.getParameter("test_id");
		String r=trans_2_json_atts_onload(g_test_id);
		return r;
	}
	
	private String trans_2_json_atts_onload(String g_test_id) {
		String old_att="";
		List<Attachment> att_list=att_Service.search_old_atts_by_test_id(g_test_id);
		if (att_list != null) {
			for (int i = 0; i < att_list.size(); i++) {
				Attachment att_tmp = att_list.get(i);
				if (i==att_list.size()) {
					old_att=old_att+"{type:'block',list:["
											+"{type : 'label', name:'"+att_tmp.getAtt_file_name()+"',labelWidth:'200', label:'"+att_tmp.getAtt_file_name()+"'},"
											+"{type : 'newcolumn'},"
											+"{type : 'button',name : 'download_"+att_tmp.getId()+"',value : '下载'}"
									+"]}";	
				} else {
					old_att=old_att+"{type:'block',list:["
											+"{type : 'label', name:'"+att_tmp.getAtt_file_name()+"',labelWidth:'200', label:'"+att_tmp.getAtt_file_name()+"'},"
											+"{type : 'newcolumn'},"
											+"{type : 'button',name : 'download_"+att_tmp.getId()+"',value : '下载'}"
									+"]},";	

				}
				
			}
		}
		String return_s="";
		if (!old_att.equals("")) {
			return_s="[{type: 'fieldset', label: '接收时已上传附件',  list:["
					+old_att	
					+"]}]";
		} 
				
		return return_s;
	}

	@RequestMapping(value = "/test_assign_save_opt")
	public boolean test_assign_save_opt(HttpServletRequest request,Model model,Test_base_info test_base_info) {
		logger.info("test_assign_save_opt");
		// model.addAttribute("name", name);
		model.addAttribute("Test_base_info",test_base_info);	
		boolean r=test_bill_Service.save_test_base_info(test_base_info);
		return r;
	}

	@RequestMapping(value = "/test_assign_select_req_grid", method = { RequestMethod.GET })
	public String test_assign_select_req_grid(HttpServletRequest request) {

		// model.addAttribute("name", name);
		String params = request.getParameter("params");
		logger.info("test_assign_select_req_grid params " + params);
		String r_string = trans_2_json_test_assign_grid(params);

		if (r_string != null) {
			// String
			// s_tmp="req_bill_no:"+r.get(0).getReq_bill_no()+";req_title:"+r.get(0).getReq_title()+";req_type:"+r.get(0).getReq_type()+";req_dept:"+r.get(0).getReq_offer_dept()+";req_complete_date:"+r.get(0).getRequired_completion_date();

			logger.info("test_assign_select_req_grid return :" + r_string);
			return r_string;
		} else {
			return null;
		}

	}

	private String trans_2_json_test_assign_grid(String params) {
		// String[] s=params.split(",", 3);
		// List<Req_bill> result=false;

		String[] s_array = params.split(",",3);
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("req_titile", s_array[0]);

		map.put("req_bill_no", s_array[1]);

		map.put("technical_director", s_array[2]);
		
		List<Req_bill> r = req_bill_Service.req_search_for_test_assign_grid(map);

		if (r != null) {
			// return r;
			String s2 = trans_2_json(r);		
			return s2;
		} else {
			return null;
		}
	}

	private String trans_2_json(List<Req_bill> obj) {
		String r_start = "{rows:[";
		String r_end = "]}";
		String r_content = "";

		for (int i = 0; i < obj.size(); i++) {
			Req_bill element = obj.get(i);
			String tmp_start = "{ id:" + (i + 1) + ",data:[";
			String tmp_end = "]}";
			if ((i + 1) < obj.size()) {
				tmp_end = tmp_end + ",";
			}

			String tmp_content = "'0',";
			Test_bill tmp_test_bill=test_bill_Service.search_a_test_bill(String.valueOf(element.getId()));
			//String tmp_content = "";
			tmp_content = tmp_content + "'" + tmp_test_bill.getId() + "',";
			tmp_content = tmp_content + "'" + element.getReq_bill_no() + "',";
			tmp_content = tmp_content + "'" + element.getReq_type() + "',";
			tmp_content = tmp_content + "'" + element.getReq_title() + "',";
			tmp_content = tmp_content + "'" + element.getReq_offer_dept() + "',";
			tmp_content = tmp_content + "'" + element.getDeffect_no() + "',";
			tmp_content = tmp_content + "'" + element.getReq_status() + "'";
			r_content = r_content + tmp_start + tmp_content + tmp_end;
		}
		String r_s = r_start + r_content + r_end;
		logger.info("trans_2_json :" + r_s);
		return r_s;
	}
	
	@RequestMapping(value = "/test_assign_form_onload", method = { RequestMethod.GET })
	public String test_assign_form_onload(HttpServletRequest request) {
		String g_test_id= request.getParameter("test_id");
		String return_s="";
		//如果g_req_id为空时
		if (g_test_id.equals("")) {
			return_s=trans_2_json_test_assign_form();
		} else {			
			return_s=trans_2_json_test_assign_form(g_test_id);
		}
		return return_s;				
	}

	private String trans_all_user_2_json(List<User> list_u) {
		String json_s="";
		for (int i = 0; i < list_u.size(); i++) {
			User u = list_u.get(i);
			String tmp=u.getName();
			if(i==list_u.size()){
				json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'}";
			}else{
				json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'},";
			}
		}		
		
		return json_s;
	}
	
	private String trans_2_json_test_assign_form(String g_test_id) {
		
		//Req_base_info req_base_info=req_bill_Service.search_req_base_info(g_req_id);
		Test_bill test_bill=test_bill_Service.search_a_test_bill_by_id(g_test_id);
		Req_bill req_bill=req_bill_Service.req_bill_search_by_id(String.valueOf(test_bill.getReq_id()));
		
		String getTechnical_director="";
		if(test_bill!=null){
			getTechnical_director=test_bill.getTest_technical_director();
		}
		
		List<User> list_u=userService.getAllUserList();
		String json_all_user=trans_all_user_2_json(list_u);		
		String json_test_head="{text: '', value: ''},"+json_all_user;
		
		Date g_date= test_bill.getTest_complete_date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String getTest_complete_date=sdf.format(g_date);
		//String g_test_guide=test_bill.getTest_guide();
		//String g_Req_remark=test_bill.getReq_remark();
		String g_test_guide="";
		String g_Req_remark="";
		
		String json_test_executor_filter_user="";
		String json_test_executor="";
		String json_test_executor_value="";
		Test_base_info test_base_info=test_bill_Service.search_a_test_base_info_by_testid(g_test_id);
			if(test_base_info!=null){
				/*json_test_executor=test_base_info.getTest_executor();
				String[] tmp1=json_test_executor.split(",");
				for (int i = 0; i < tmp1.length; i++) {
					if(i==tmp1.length){
						json_test_executor_value=json_test_executor_value+"{value : '"+tmp1[i]+"',text:'"+tmp1[i]+"'}";
					}
					else{
						json_test_executor_value=json_test_executor_value+"{value : '"+tmp1[i]+"',text:'"+tmp1[i]+"'},";
					}
				}
				json_test_executor_filter_user=filter_user_2_json(list_u,json_test_executor);*/
			}else{
				json_test_executor_filter_user=json_all_user;
			}		
			
		String s_report=get_test_report_status_v(test_bill);
		String r_start="["
				+"{type:'block',list:["
					+ "{type: 'fieldset', label: '查询',  list:["
							+"{type:'block',list:["
								+"{type : 'input',name : 'query_req_title',label : '主题关键字',inputWidth:'400',labelWidth:'70',inputWidth:'300',position:'label-left'}"							
							+"]},"
							+"{type:'block',list:["
								+"{type : 'input',name : 'query_req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left'},"
								+"{type : 'newcolumn'},"
								+"{type : 'input',name : 'query_technical_director',label : '技术负责人',inputWidth:'150',position:'label-left', offsetLeft:'25'}"										
							+"]},"
							+"{type:'block',list:["
								+"{type : 'button',name : 'btn_select_req',value : '查询',offsetLeft:'25'}"
							+"]}"
						+"]}"
				+"]},"
				+ "{type: 'fieldset', label: '测试单',  list:["
								+"{type:'block',list:["
									+"{type : 'hidden',name : 'req_id',label : '需求ID',labelWidth:'70',position:'label-left', disabled:'true'},"
									+"{type : 'hidden',name : 'test_id',label : '测试ID',labelWidth:'70',position:'label-left', disabled:'true'}"
								+"]},"
								+"{type:'block',list:["
									+"{type : 'input',name : 'att_group_id',label : 'att_group_id',inputWidth:'150',position:'label-left', hidden:'true',offsetLeft:'25'},"
									+"{type : 'input',name : 'att_id',label : 'att_id',inputWidth:'150',position:'label-left', hidden:'true', offsetLeft:'25'}"										
								+"]},"
																	
								+"{type:'block',list:["
									+"{type : 'input',name : 'req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left',  required:'true',disabled:'true',value:'"+req_bill.getReq_bill_no()+"'},"
									+"{type : 'newcolumn'},"
									+"{type : 'input',name : 'test_technical_director',label : '测试技术负责人',inputWidth:'150',position:'label-left', disabled:'true',offsetLeft:'25',value:'"+getTechnical_director+"'},"
									+"{type : 'newcolumn'},"
									+"{type : 'select',name : 'test_report_status',label : '自测试报告',inputWidth:'150',  disabled:'true',offsetLeft:'10',options:["
										+s_report
									+ "]}"
								+"]},"
								+"{type:'block',list:["
										+"{type : 'input',name : 'test_workload',label : '测试工作量',inputWidth:'100',numberFormat: '0,000.0',required:'true',validate:'check_workload',position:'label-left'},"
										+"{type : 'newcolumn'},"
										+"{type : 'input',name : 'test_workload_day',label : '人天',inputWidth:'1',disabled:'true',position:'label-left'},"
										+"{type : 'newcolumn'},"
										+"{type : 'calendar',name : 'test_plan_date',label : '计划完成日',position:'label-left', inputWidth:'100',required:'true',offsetLeft:'50'},"
										+"{type : 'newcolumn'},"
										+"{type : 'input',name : 'test_complete_date',label : '要求完成时间',inputWidth:'100',labelWidth:'100',position:'label-left', offsetLeft:'75', disabled:'true',value:'"+getTest_complete_date+"'}"
								+"]},"
								+"{type:'block',list:["										
										+"{type : 'select',name : 'test_difficult',label : '测试难度',inputWidth:'150', required:'true',options:["
													+"{text: '', value: ''},"
													+"{text: '难', value: '难'},"													
													+"{text: '中',value: '中'},"
													+"{text: '易', value: '易'}"
										+ "]},"
										+"{type : 'newcolumn'},"													
										+"{type: 'combo', label: '测试负责人', name: 'test_head', inputWidth:'150',required:'true',offsetLeft:'25',options:["
												+json_test_head
				 								+"],"
		 								+ "},"
		 								+"{type : 'newcolumn'},"
		 								+"{type:'multiselect', label: '测试实施人',position: 'label-top',name:'test_executor', required:'true',size:5,offsetLeft:'25', width:80,options:["
		 											+json_test_executor_value
		 												+"]},"
										+"{type : 'newcolumn'},"
										+"{type:'block',list:["
											+"{type: 'button', name: 'add3', value: '>>', width:30,offsetTop: 30},"
											+"{type: 'button', name: 'remove3', width:30,value:'<<'}"
										+"]},"
										+"{type : 'newcolumn'},"
										+"{type:'multiselect', name:'test_executor_all', size:5,width:80, offsetTop: 30,options:["						
											+json_test_executor_filter_user
										+"]}"								
								+"]},"
								
								+"{type:'block',list:["
									+"{type : 'input',name : 'test_introduction',id : 'test_guide',label : '测试指引',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',value:'"+g_test_guide+"',offsetLeft:'25'},"
									+"{type : 'input',name : 'test_remark',label : '其他说明',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',value:'"+g_Req_remark+"',offsetLeft:'25'}"										
								+"]}"										
								/*+"{type:'block',list:["
									+"{type : 'editor',name : 'test_introduction',label : '测试指引',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'},"
									+"{type : 'editor',name : 'test_remark',label : '其他说明',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'}"										
								+"]},"
								+"{type:'fieldset',label:'附件',list:["
									+"{type : 'label', name:'att_label', label:'上传文件最大值: 100 Mb'},"
									+"{type:'upload', name:'myFiles', inputWidth:'400', titleText:'拖拽文件到此区域或点击选择文件上传', url:'/attachment/upload', _swfLogs:'enabled',  autoStart:'true', swfPath:'/common/uploader.swf',  swfUrl:'attachment/upload'}"
								+"]},"	
							
								+"{type:'block',list:["
									+"{type : 'button',name : 'test_assign_save_btn',value : '分配'}"																	
								+"]}"*/											
								//+"{type:'block',list:["
								//	+"{type : 'input',name : 'req_title',label : '需求主题',labelWidth:'70', inputWidth='700',position:'label-left', disabled:'true',value:'"+obj.getReq_title()+"'}"
								//+"]}"
							+"]"
					+ "}"
				+"]";
		String r_s=r_start;
		return r_s;
		
	
	}

	private String get_test_report_status_v(Test_bill test_bill) {
		String tmp=test_bill.getTest_report_status();
		String r_v="";
		if (tmp.equals("")){
			r_v="{text: '', value: ''},{text: '有', value: '有'},{text: '无',value: '无'}";
		}else{
			r_v="{text: '', value: ''},";
			if(tmp.equals("有")){
				r_v=r_v+"{text: '有', value: '有',selected:'true'},{text: '无',value: '无'}";		
			}else{
				r_v=r_v+"{text: '有', value: '有'},{text: '无',value: '无',selected:'true'}";					
			}
		}
			
		return r_v;
	}

	private String trans_2_json_test_assign_form() {
		String r_start="["
				+"{type:'block',list:["
					+ "{type: 'fieldset', label: '查询',  list:["
							+"{type:'block',list:["
								+"{type : 'input',name : 'query_req_title',label : '主题关键字',inputWidth:'400',labelWidth:'70',inputWidth:'300',position:'label-left'}"							
							+"]},"
							+"{type:'block',list:["
								+"{type : 'input',name : 'query_req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left'},"
								+"{type : 'newcolumn'},"
								+"{type : 'input',name : 'query_technical_director',label : '技术负责人',inputWidth:'150',position:'label-left', offsetLeft:'25'}"										
							+"]},"
							+"{type:'block',list:["
								+"{type : 'button',name : 'btn_select_req',value : '查询',offsetLeft:'25'}"
							+"]}"
					+"]},"
					
				+"]},"			
				+ "{type: 'fieldset', label: '测试单',  list:["
								+"{type:'block',list:["
									+"{type : 'hidden',name : 'req_id',label : '需求ID',labelWidth:'70',position:'label-left', disabled:'true'}"
								+"]},"
								+"{type:'block',list:["
									+"{type : 'input',name : 'att_group_id',label : 'att_group_id',inputWidth:'150',position:'label-left', hidden:'true',offsetLeft:'25'},"
									+"{type : 'input',name : 'att_id',label : 'att_id',inputWidth:'150',position:'label-left', hidden:'true', offsetLeft:'25'}"										
								+"]},"
																	
								+"{type:'block',list:["
									+"{type : 'input',name : 'req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left',required:'true', disabled:'true'},"
									+"{type : 'newcolumn'},"
									+"{type : 'input',name : 'technical_director',label : '测试技术负责人',inputWidth:'150',position:'label-left', disabled:'true',offsetLeft:'25'},"
									+"{type : 'newcolumn'},"
									+"{type : 'select',name : 'test_report_status',label : '自测试报告',inputWidth:'150',disabled:'true', options:["
											+"{text: '', value: ''},"
											+"{text: '有', value: '有'},"													
											+"{text: '无',value: '无'}"
									+ "]}"
								+"]},"
								+"{type:'block',list:["
									+"{type : 'input',name : 'req_bill_no',label : '测试负责人',inputWidth:'150',position:'label-left',required:'true' },"
									+"{type : 'newcolumn'},"
									+"{type : 'input',name : 'req_bill_no',label : '测试实施人',inputWidth:'150',position:'label-left', required:'true'},"
									+"{type : 'newcolumn'},"
									+"{type : 'select',name : 'test_report_status',label : '测试难度',inputWidth:'150', required:'true',options:["
											+"{text: '', value: ''},"
											+"{text: '难', value: '难'},"													
											+"{text: '中',value: '中'},"
											+"{text: '易', value: '易'}"
									+ "]}"
								+"]},"
								+"{type:'block',list:["
									+"{type : 'input',name : 'test_workload',label : '测试工作量',inputWidth:'150',position:'label-left', validate:'check_workload',required:'true'},"
									+"{type : 'newcolumn'},"
									+"{type : 'calendar',name : 'test_plan_date',label : '计划完成日',position:'label-left', required:'true'}"
								+"]},"
								+"{type:'block',list:["
									+"{type : 'input',name : 'test_guide',id : 'test_guide',label : '测试指引',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',offsetLeft:'25'},"
									+"{type : 'input',name : 'req_remark',label : '其他说明',inputWidth:'150',position:'label-top', hidden:'true',rows:'3',offsetLeft:'25'}"										
								+"]}"
								/*+"{type:'block',list:["
									+"{type : 'editor',name : 'test_guide',label : '测试指引',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'},"
									+"{type : 'editor',name : 'req_remark',label : '其他说明',position:'label-top', required:'true',inputWidth:'800', inputHeight:'200'}"										
								+"]},"
								+"{type:'fieldset',label:'附件',list:["
									+"{type : 'label', name:'att_label', label:'上传文件最大值: 100 Mb'},"
									+"{type:'upload', name:'myFiles', inputWidth:'400', titleText:'拖拽文件到此区域或点击选择文件上传', url:'/attachment/upload', _swfLogs:'enabled',  autoStart:'true', swfPath:'/common/uploader.swf',  swfUrl:'attachment/upload'}"
								+"]},"	
							
								+"{type:'block',list:["
									+"{type : 'button',name : 'test_assign_save_btn',value : '分配'}"																	
								+"]}"*/											
								//+"{type:'block',list:["
								//	+"{type : 'input',name : 'req_title',label : '需求主题',labelWidth:'70', inputWidth='700',position:'label-left', disabled:'true',value:'"+obj.getReq_title()+"'}"
								//+"]}"
							+"]"
					+ "}"
				+"]";
		String r_s=r_start;
		return r_s;
	}
	
	
	@RequestMapping(value = "/test_assign_list_form_load", method = { RequestMethod.GET })
	public String test_assign_list_form_load(HttpServletRequest request) {

		
		logger.info("test_assign_list_form_load" );
		String r_string = trans_2_json_test_assign_list_form_load();

		if (r_string != null) {			
			logger.info("test_assign_list_form_load return :" + r_string);
			return r_string;
		} else {
			return null;
		}

	}

	private String trans_2_json_test_assign_list_form_load() {
		String r_start="["
				+"{type:'block',list:["
					+ "{type: 'fieldset', label: '查询',  list:["
							+"{type:'block',list:["
								+"{type : 'input',name : 'query_req_title',label : '主题关键字',inputWidth:'300',labelWidth:'70',inputWidth:'300',position:'label-left'}"
								
							+"]},"
							+"{type:'block',list:["
								+"{type : 'input',name : 'query_req_bill_no',label : '需求编号',inputWidth:'150',labelWidth:'70',position:'label-left'},"
								+"{type : 'newcolumn'},"
								+"{type : 'input',name : 'query_technical_director',label : '测试技术负责人',inputWidth:'150',position:'label-left', offsetLeft:'50'}"										
							+"]},"
							+"{type:'block',list:["								
								+"{type : 'button',name : 'query_btn',value : '查询',offsetLeft:'25'}"
							+"]}"
					+"]}"					
				+"]}"				
			
				+"]";
		String r_s=r_start;
		return r_s;
	}
	
}
